package com.example.mobile2021606159yhf0525;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private EditText edtTextMainUserName;
    private EditText edtTextMainPassword;
    private CheckBox chkBoxMainPSW;
    private CheckBox chkBoxMainLogin;
    private Button btnMianLogin;
    private Button btnMainRegedit;
    private String url = "http://121.9.253.237:8888/traffic/api/v2/user_login";
    OkHttpClient okHttpClient=new OkHttpClient();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnMianLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userName=edtTextMainUserName.getText().toString().trim();
                String password=edtTextMainPassword.getText().toString().trim();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        JsonObject jsonObject=new JsonObject();
                        jsonObject.addProperty("UserName",userName);
                        jsonObject.addProperty("UserPwd",password);
                        String json=jsonObject.toString();
                        MediaType mediaType=MediaType.Companion.parse("application/json;charset=utf-8");
                        RequestBody requestBody=RequestBody.create(json,mediaType);
                        Request request=new Request.Builder()
                                .url(url)
                                .post(requestBody)
                                .build();
                        Call call=okHttpClient.newCall(request);
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {

                            }

                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                String result=response.body().string();
                                Gson gson=new Gson();
                                LoginBean loginBean=gson.fromJson(result,LoginBean.class);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (loginBean.getRESULT().equals("S")){
                                            new AlertDialog.Builder(MainActivity.this)
                                                    .setMessage("登录成功")
                                                    .create()
                                                    .show();
                                        }else if(loginBean.getRESULT().equals("F")){
                                            new AlertDialog.Builder(MainActivity.this)
                                                    .setMessage("登录失败")
                                                    .create()
                                                    .show();
                                        }
                                    }
                                });
                                Log.i("TAG","onResponse:"+loginBean.getRESULT());

                            }
                        });
                    }
                }).start();
            }
        });
    }
    private void initView(){
        edtTextMainPassword=(EditText)findViewById(R.id.edtTextMainPassword);
        edtTextMainUserName=(EditText)findViewById(R.id.edtTextMainUserName);
        chkBoxMainLogin=(CheckBox)findViewById(R.id.checkBoxMainLogin);
        chkBoxMainPSW=(CheckBox)findViewById(R.id.checkBoxMainPSW);
        btnMainRegedit=(Button)findViewById(R.id.btnMainRegedit);
        btnMianLogin=(Button)findViewById(R.id.btnMainLogin);
    }
}